/*
 * Algorithms.cpp
 *
 *  Created on: 22 Aug 2011
 *      Author: Allan
 */

#include "Algorithms.h"

const unsigned 
IndexOf(const string& word, const vector<string>& words)
{
	return find(words.begin(), words.end(), word) - words.begin();
}

const bool 
IsContained(const string& word, const vector<string>& words)
{
	return IndexOf(word, words) < words.size();
}

const vector<string> 
Union(const vector<string>& words1, const vector<string>& words2)
{
	set<string> set;
	
	set.insert(words1.begin(), words1.end());
	set.insert(words2.begin(), words2.end());
	
	return vector<string>(set.begin(), set.end());
}

const vector<string> 
Intersection(const vector<string>& words1, const vector<string>& words2)
{
	vector<string> intersection;
	
	BOOST_FOREACH(auto s, words1)
		if(IsContained(s, words2))
			intersection.push_back(s);
	
	return intersection;
}

const vector<string> 
Difference(const vector<string>& words1, const vector<string>& words2)
{
	vector<string> difference;
	
	BOOST_FOREACH(auto s, words1)
		if(!IsContained(s, words2))
			difference.push_back(s);
	
	return difference;
}

const vector<string> 
Unique(const vector<string>& words)
{
	set<string> set(words.begin(), words.end());
	
	return vector<string>(set.begin(), set.end());
}

